package cn.shadow.module.scene;


import cn.shadow.utils.Utils;

/**
 * 所有场景格子
 */
public class ViewGrids {

    public int maxXIdx;
    public int maxYIdx;
    public int viewGridLength;
    public ViewGrid[][] grids;

    public ViewGrids(int width, int height, boolean isSeeAll) {
        if (isSeeAll) {
            this.viewGridLength = 1 << 30;
        } else {
            this.viewGridLength = 600;
        }

        int xCount = Utils.divide(width, viewGridLength);
        assert xCount >= 1;
        int yCount = Utils.divide(height, viewGridLength);
        assert yCount >= 1;

        this.maxXIdx = xCount - 1;
        this.maxYIdx = yCount - 1;

        this.grids = new ViewGrid[yCount][xCount];
        for (int yIdx = 0; yIdx < grids.length; yIdx++) {
            ViewGrid[] gridArray = grids[yIdx];
            for (int xIdx = 0; xIdx < gridArray.length; xIdx++) {
                gridArray[xIdx] = new ViewGrid(xIdx, yIdx, isSeeAll, viewGridLength);
            }
        }
    }

    /**
     * 通过坐标获得视野格子
     */
    public ViewGrid getViewGrid(int x, int y) {
        int xIdx = x / viewGridLength;
        int yIdx = y / viewGridLength;
        // 给一个边界格子
        return getViewGridByIdx(xIdx, yIdx);
    }

    public ViewGrid getViewGridByIdx(int xIdx, int yIdx) {
        return Utils.getValidObject(Utils.getValidObject(grids, yIdx), xIdx);
    }
}
